package com.program.facesigninsystem.service.impl;

import com.program.facesigninsystem.entity.User;
import com.program.facesigninsystem.entity.UserInfo;
import com.program.facesigninsystem.service.IRedisService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sxw
 */
@Service
public class RedisServiceImpl implements IRedisService {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void addUser(String uid, String ip, String loginTime, String errorCounts) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        if(map==null) {
            map= new HashMap<>();
        }
        map.put("uid",uid);
        map.put("ip",ip);
        map.put("loginTime",loginTime);
        map.put("errorCounts",errorCounts);
        map.put("nextLogin","");
        vo.set(uid, map);
    }

    @Override
    public void addUserInfo(UserInfo userInfo,String Class) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(userInfo.getUid());
        if(map==null) {
            map= new HashMap<>();
        }
        map.put("uid",userInfo.getUid());
        map.put("openid",userInfo.getOpenid());
        map.put("type",userInfo.getType()+"");
        map.put("name",userInfo.getName());
        map.put("sex",userInfo.getSex());
        map.put("age",userInfo.getAge()+"");
        map.put("address",userInfo.getAddress());
        map.put("phone",userInfo.getPhone());
        map.put("email",userInfo.getEmail());
        map.put("university",userInfo.getUniversity());
        map.put("academy",userInfo.getAcademy());
        map.put("major",userInfo.getMajor());
        map.put("class",Class);
        map.put("avatar",userInfo.getAvatar());
        map.put("uid1",userInfo.getUid1());
        map.put("bindId",userInfo.getBindId()+"");
        vo.set(userInfo.getUid(), map);
    }

    @Override
    public User findByUId(String uid) {
        ValueOperations vo=redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        if(map!=null){
            String ip=(String)map.get("ip");
            String loginTime=(String)map.get("loginTime");
            String errorCounts=(String)map.get("errorCounts");
            String nextLogin=(String)map.get("nextLogin");
            User user=new User(uid,ip,loginTime,errorCounts,nextLogin);
            return user;
        }else {
            return null;
        }
    }

    @Override
    public UserInfo findUser(String uid) {
        ValueOperations vo=redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        if(map!=null){
            String openid=(String)map.get("openid");
            String name =(String)map.get("name");
            int type=Integer.parseInt((String)map.get("type"));
            String sex=(String)map.get("sex");
            int age=Integer.parseInt((String)map.get("age"));
            String address=(String)map.get("address");
            String phone=(String)map.get("phone");
            String email=(String)map.get("email");
            String university=(String)map.get("university");
            String academy=(String)map.get("academy");
            String major=(String)map.get("major");
            String Class=(String)map.get("class");
            String avatar=(String)map.get("avatar");
            String uid1=(String)map.get("uid1");
            int bindId=Integer.parseInt((String)map.get("bindId"));
            UserInfo userInfo=new UserInfo(uid,openid,name,type,sex,age,address,phone,email,university,academy,major,Class,avatar,uid1,bindId);
            return userInfo;
        }else {
            return null;
        }
    }

    @Override
    public void updateBindid(String uid,String bindid) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        assert map != null;
        if (map.containsKey("bindId")){
            map.put("bindId",bindid);
        }
        vo.set(uid, map);
    }

    @Override
    public void updateUser(String uid, String name, int type, String major, String uclass, String university, String academy, String avatar, String sex, int age, String address, String phone, String email) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        assert map != null;
        if (map.containsKey("name")){
            map.put("name",name);
        }
        if(map.containsKey("type")){
            map.put("type",type+"");
        }
        if (map.containsKey("major")){
            map.put("major",major);
        }
        if(map.containsKey("class")){
            map.put("class",uclass);
        }
        if (map.containsKey("university")){
            map.put("university",university);
        }
        if(map.containsKey("academy")){
            map.put("academy",academy);
        }
        if (map.containsKey("avatar")){
            map.put("avatar",avatar);
        }
        if(map.containsKey("sex")){
            map.put("sex",sex);
        }
        if (map.containsKey("age")){
            map.put("age",age+"");
        }
        if(map.containsKey("address")){
            map.put("address",address);
        }
        if (map.containsKey("phone")){
            map.put("phone",phone);
        }
        if(map.containsKey("email")){
            map.put("email",email);
        }
        vo.set(uid, map);
    }

    @Override
    public void updateByUId(String uid, String ip, String loginTime) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        assert map != null;
        if (map.containsKey("ip")){
            map.put("ip",ip);
        }
        if(map.containsKey("loginTime")){
            map.put("loginTime",loginTime);
        }
        vo.set(uid, map);
    }

    @Override
    public void updateLogin(String uid, String errorCounts, String nextLogin) {
        ValueOperations vo = redisTemplate.opsForValue();
        Map map=(Map)vo.get(uid);
        assert map != null;
        if (map.containsKey("errorCounts")){
            map.put("errorCounts",errorCounts);
        }
        if(map.containsKey("nextLogin")){
            map.put("nextLogin",nextLogin);
        }
        vo.set(uid, map);
    }

    @Override
    public void addRank(List<Map<String, Object>> rank) {
        ValueOperations vo = redisTemplate.opsForValue();
        vo.set("Rank", rank);
    }

    @Override
    public List<Map<String, Object>> getRank() {
        ValueOperations vo=redisTemplate.opsForValue();
        List<Map<String, Object>> rank=(List<Map<String, Object>>)vo.get("Rank");
        if(rank!=null){
            return rank;
        }else {
            return null;
        }
    }
}
